package com.szqhyxj.youxiangju.biz.card.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.card.CardModel;
import com.szqhyxj.youxiangju.biz.card.credit.CreaditDetailsActivity;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CardAdapter extends BaseAdapter{

    private List<CardModel.Card> cards;
    private Context mContext;
    public CardAdapter(Context context, List<CardModel.Card> cards){
        this.cards = cards;
        mContext = context;
    }

    @Override
    public int getCount(){
        return cards.size();
    }

    @Override
    public Object getItem(int i){
        return cards.get(i);
    }

    @Override
    public long getItemId(int i){
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup){
        if(view == null){
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_list_card_item, null);
            view.setTag(new ViewHolder(view));
        }
        CardModel.Card card = cards.get(i);
        ViewHolder viewHolder = (ViewHolder) view.getTag();

        viewHolder.name.setText(card.getBtName());
        String cardNo = card.getBankCardNo();
        int leng = cardNo.length();
        viewHolder.cardEnd.setText(cardNo.substring(leng-4));
        LoadImageUtils.loadImage(viewHolder.tubiao,card.getCardLogo());

        view.setOnClickListener(new OnBtnClickListener(i));
        return view;
    }

    private class OnBtnClickListener implements View.OnClickListener{
        private int i;

        public OnBtnClickListener(int i){
            this.i = i;
        }

        @Override
        public void onClick(View view){
            CreaditDetailsActivity.start((Activity) mContext,cards.get(i).getCardId());
        }
    }

    static class ViewHolder{
        @BindView(R.id.tubiao)
        ImageView tubiao;
        @BindView(R.id.name)
        TextView name;

        @BindView(R.id.cardEnd)
        TextView cardEnd;

        ViewHolder(View view){
            ButterKnife.bind(this, view);
        }
    }
}
